﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Lambda3.VisualStudio.DataTools.Generators.Tests
{
    [TestClass]
    public class CpfTest
    {
        [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void Cpf_Cannot_Be_Negative_Number()
        {
            var cpf = new VisualStudio.DataTools.Generators.Cpf.Cpf(-1);
        }

        [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void Cpf_Cannot_Be_Zero()
        {
            var cpf = new VisualStudio.DataTools.Generators.Cpf.Cpf(0);
        }

        [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void Cpf_Must_Be_Less_Than_Max_Cpf_Size()
        {
            var cpf = new VisualStudio.DataTools.Generators.Cpf.Cpf(1000000000);
        }

        [TestMethod]
        public void Generate_Valid_Check_Digits()
        {
            // CPFs generated from www.geradorcpf.com

            var cpf = new VisualStudio.DataTools.Generators.Cpf.Cpf(123123123);
            Assert.AreEqual(87, cpf.Digits);
        }

        [TestMethod]
        public void Generate_Cpf_String_Without_Formatting()
        {
            var cpf = new VisualStudio.DataTools.Generators.Cpf.Cpf(123123123, false, false);
            Assert.AreEqual("12312312387", cpf.ToString());
        }

        [TestMethod]
        public void Generate_Cpf_String_With_Group_Separators()
        {
            var cpf = new VisualStudio.DataTools.Generators.Cpf.Cpf(123123123, true, false);
            Assert.AreEqual("123.123.12387", cpf.ToString());
        }

        [TestMethod]
        public void Generate_Cpf_String_With_Digit_Separators()
        {
            var cpf = new VisualStudio.DataTools.Generators.Cpf.Cpf(123123123, false, true);
            Assert.AreEqual("123123123-87", cpf.ToString());
        }

        [TestMethod]
        public void Generate_Cpf_String_With_All_Separators()
        {
            var cpf = new VisualStudio.DataTools.Generators.Cpf.Cpf(123123123, true, true);
            Assert.AreEqual("123.123.123-87", cpf.ToString());
        }

        [TestMethod]
        public void Generated_Cpf_Is_Valid()
        {
            var cpf = new VisualStudio.DataTools.Generators.Cpf.Cpf(new Random(1).Next(999999999), false, false);
            Assert.IsTrue(IsCpfValid(cpf.ToString()));
        }

        /// <summary>
        /// Checks whether the CPF is valid
        /// </summary>
        /// <remarks>
        /// Sample copied from http://social.msdn.microsoft.com/Forums/pt/vscsharppt/thread/1dbe81e6-c063-4ae5-ae1d-5643fb4b0e62
        /// </remarks>
        /// <param name="cpf">A CPF string</param>
        /// <returns>True if the parameter represents a valid CPF</returns>
        public bool IsCpfValid(string cpf)
        {

            int[] multiplicador1 = new int[9] { 10, 9, 8, 7, 6, 5, 4, 3, 2 };

            int[] multiplicador2 = new int[10] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 };

            string tempCpf;

            string digito;

            int soma;

            int resto;

            cpf = cpf.Trim();

            cpf = cpf.Replace(".", "").Replace("-", "");

            if (cpf.Length != 11)

                return false;

            tempCpf = cpf.Substring(0, 9);

            soma = 0;

            for (int i = 0; i < 9; i++)

                soma += int.Parse(tempCpf[i].ToString()) * multiplicador1[i];

            resto = soma % 11;

            if (resto < 2)

                resto = 0;

            else

                resto = 11 - resto;

            digito = resto.ToString();

            tempCpf = tempCpf + digito;

            soma = 0;

            for (int i = 0; i < 10; i++)

                soma += int.Parse(tempCpf[i].ToString()) * multiplicador2[i];

            resto = soma % 11;

            if (resto < 2)

                resto = 0;

            else

                resto = 11 - resto;

            digito = digito + resto.ToString();

            return cpf.EndsWith(digito);

        }


    }
}
